package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.PersonaRolDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class PersonaRolView {
    private InputText txtCodigoPer_Persona;
    private InputText txtCodigoPro_Proyecto;
    private InputText txtCodigoRol_Rol;
    private InputText txtCodigoPer;
    private InputText txtCodigoRol;
    private InputText txtCodigoPro;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<PersonaRolDTO> data;
    private PersonaRolDTO selectedPersonaRol;

    public PersonaRolView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            PersonaRolDTO personaRolDTO = (PersonaRolDTO) e.getObject();

            if (txtCodigoPer_Persona == null) {
                txtCodigoPer_Persona = new InputText();
            }

            txtCodigoPer_Persona.setValue(personaRolDTO.getCodigoPer_Persona());

            if (txtCodigoPro_Proyecto == null) {
                txtCodigoPro_Proyecto = new InputText();
            }

            txtCodigoPro_Proyecto.setValue(personaRolDTO.getCodigoPro_Proyecto());

            if (txtCodigoRol_Rol == null) {
                txtCodigoRol_Rol = new InputText();
            }

            txtCodigoRol_Rol.setValue(personaRolDTO.getCodigoRol_Rol());

            if (txtCodigoPer == null) {
                txtCodigoPer = new InputText();
            }

            txtCodigoPer.setValue(personaRolDTO.getCodigoPer());

            if (txtCodigoRol == null) {
                txtCodigoRol = new InputText();
            }

            txtCodigoRol.setValue(personaRolDTO.getCodigoRol());

            if (txtCodigoPro == null) {
                txtCodigoPro = new InputText();
            }

            txtCodigoPro.setValue(personaRolDTO.getCodigoPro());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtCodigoPer_Persona != null) {
            txtCodigoPer_Persona.setValue(null);
            txtCodigoPer_Persona.setDisabled(true);
        }

        if (txtCodigoPro_Proyecto != null) {
            txtCodigoPro_Proyecto.setValue(null);
            txtCodigoPro_Proyecto.setDisabled(true);
        }

        if (txtCodigoRol_Rol != null) {
            txtCodigoRol_Rol.setValue(null);
            txtCodigoRol_Rol.setDisabled(true);
        }

        if (txtCodigoPer != null) {
            txtCodigoPer.setValue(null);
            txtCodigoPer.setDisabled(false);
        }

        if (txtCodigoRol != null) {
            txtCodigoRol.setValue(null);
            txtCodigoRol.setDisabled(false);
        }

        if (txtCodigoPro != null) {
            txtCodigoPro.setValue(null);
            txtCodigoPro.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        PersonaRol entity = null;

        try {
            PersonaRolId id = new PersonaRolId();
            id.setCodigoPer((((txtCodigoPer.getValue()) == null) ||
                (txtCodigoPer.getValue()).equals("")) ? null
                                                      : new String(
                    txtCodigoPer.getValue().toString()));
            id.setCodigoRol((((txtCodigoRol.getValue()) == null) ||
                (txtCodigoRol.getValue()).equals("")) ? null
                                                      : new String(
                    txtCodigoRol.getValue().toString()));
            id.setCodigoPro((((txtCodigoPro.getValue()) == null) ||
                (txtCodigoPro.getValue()).equals("")) ? null
                                                      : new String(
                    txtCodigoPro.getValue().toString()));
            entity = BusinessDelegatorView.getPersonaRol(id);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtCodigoPer_Persona.setDisabled(false);
            txtCodigoPro_Proyecto.setDisabled(false);
            txtCodigoRol_Rol.setDisabled(false);
            txtCodigoPer.setDisabled(false);
            txtCodigoRol.setDisabled(false);
            txtCodigoPro.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtCodigoPer_Persona.setValue(entity.getPersona().getCodigoPer());
            txtCodigoPer_Persona.setDisabled(false);
            txtCodigoPro_Proyecto.setValue(entity.getProyecto().getCodigoPro());
            txtCodigoPro_Proyecto.setDisabled(false);
            txtCodigoRol_Rol.setValue(entity.getRol().getCodigoRol());
            txtCodigoRol_Rol.setDisabled(false);
            txtCodigoPer.setValue(entity.getId().getCodigoPer());
            txtCodigoPer.setDisabled(true);
            txtCodigoRol.setValue(entity.getId().getCodigoRol());
            txtCodigoRol.setDisabled(true);
            txtCodigoPro.setValue(entity.getId().getCodigoPro());
            txtCodigoPro.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.savePersonaRol(FacesUtils.checkString(
                    txtCodigoPer), FacesUtils.checkString(txtCodigoRol),
                FacesUtils.checkString(txtCodigoPro),
                FacesUtils.checkString(txtCodigoPer_Persona),
                FacesUtils.checkString(txtCodigoPro_Proyecto),
                FacesUtils.checkString(txtCodigoRol_Rol));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deletePersonaRol(FacesUtils.checkString(
                    txtCodigoPer), FacesUtils.checkString(txtCodigoRol),
                FacesUtils.checkString(txtCodigoPro));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updatePersonaRol(FacesUtils.checkString(
                    txtCodigoPer), FacesUtils.checkString(txtCodigoRol),
                FacesUtils.checkString(txtCodigoPro),
                FacesUtils.checkString(txtCodigoPer_Persona),
                FacesUtils.checkString(txtCodigoPro_Proyecto),
                FacesUtils.checkString(txtCodigoRol_Rol));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoPer == null) {
                txtCodigoPer = new InputText();
            }

            txtCodigoPer.setValue(selectedPersonaRol.getCodigoPer());

            if (txtCodigoRol == null) {
                txtCodigoRol = new InputText();
            }

            txtCodigoRol.setValue(selectedPersonaRol.getCodigoRol());

            if (txtCodigoPro == null) {
                txtCodigoPro = new InputText();
            }

            txtCodigoPro.setValue(selectedPersonaRol.getCodigoPro());

            BusinessDelegatorView.deletePersonaRol(FacesUtils.checkString(
                    txtCodigoPer), FacesUtils.checkString(txtCodigoRol),
                FacesUtils.checkString(txtCodigoPro));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedPersonaRol);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoPer, String codigoRol,
        String codigoPro, String codigoPer_Persona, String codigoPro_Proyecto,
        String codigoRol_Rol) throws Exception {
        try {
            BusinessDelegatorView.updatePersonaRol(codigoPer, codigoRol,
                codigoPro, codigoPer_Persona, codigoPro_Proyecto, codigoRol_Rol);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("PersonaRolView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtCodigoPer_Persona() {
        return txtCodigoPer_Persona;
    }

    public void setTxtCodigoPer_Persona(InputText txtCodigoPer_Persona) {
        this.txtCodigoPer_Persona = txtCodigoPer_Persona;
    }

    public InputText getTxtCodigoPro_Proyecto() {
        return txtCodigoPro_Proyecto;
    }

    public void setTxtCodigoPro_Proyecto(InputText txtCodigoPro_Proyecto) {
        this.txtCodigoPro_Proyecto = txtCodigoPro_Proyecto;
    }

    public InputText getTxtCodigoRol_Rol() {
        return txtCodigoRol_Rol;
    }

    public void setTxtCodigoRol_Rol(InputText txtCodigoRol_Rol) {
        this.txtCodigoRol_Rol = txtCodigoRol_Rol;
    }

    public InputText getTxtCodigoPer() {
        return txtCodigoPer;
    }

    public void setTxtCodigoPer(InputText txtCodigoPer) {
        this.txtCodigoPer = txtCodigoPer;
    }

    public InputText getTxtCodigoRol() {
        return txtCodigoRol;
    }

    public void setTxtCodigoRol(InputText txtCodigoRol) {
        this.txtCodigoRol = txtCodigoRol;
    }

    public InputText getTxtCodigoPro() {
        return txtCodigoPro;
    }

    public void setTxtCodigoPro(InputText txtCodigoPro) {
        this.txtCodigoPro = txtCodigoPro;
    }

    public List<PersonaRolDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataPersonaRol();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<PersonaRolDTO> personaRolDTO) {
        this.data = personaRolDTO;
    }

    public PersonaRolDTO getSelectedPersonaRol() {
        return selectedPersonaRol;
    }

    public void setSelectedPersonaRol(PersonaRolDTO personaRol) {
        this.selectedPersonaRol = personaRol;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
